//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import CampsiteBox from "../../views/Camp/CampsiteBox.vue"
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: CampsiteBox,
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  data () {
    return {
      tabledata: [],
      // getVueDictionary: []
    }
  },
  buttons: { view: [
    {
      name: "导出",
      icon: "md-share-alt",
      value: "exportBarracks",
      class: "",
      type: "",
      index: 4, //显示的位置
      onClick: function () {
        this.$refs.gridBody.model4 = true;
        this.$refs.gridBody.getyq()
      }
    }
  ], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    // 循环表单并返回数据
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    addSunmits (row) {
      // let phasedayjson = this.getcolumnsOption("phasedayjson")
      this.$refs.detail.rowData[row.index].phasedayjson = row.json
    },
    addMessagereview (row) {
      this.$refs.detail.rowData[row.index].messagereview = row.messagereview
    },
    // 循环表单并返回数据
    getcolumnsOption (field) {
      let option;

      this.detail.columns.forEach(item => {
        if (item.field == field) {
          option = item;
        }
      })
      return option;
    },
    onInit () {
      this.$store.getters.data().viewPageTest = this;
      this.maxBtnLength = 5;
      //点击单元格编辑与结束编辑(默认是点击单元格编辑，鼠标离开结束编辑)
      this.detailOptions.clickEdit = true;
      // label宽度
      this.boxOptions.labelWidth = 100
      // 弹框宽高度
      this.boxOptions.width = 1000
      this.boxOptions.height = 530;
      // 开启单选
      //;nsadsadsads
      // 内容超出后自动换行
      this.textInline = true;
      // 不显示多选框
      //;
      // 显示序号
      //;
      // 是否填写报名单
      let smsId = this.getFormOption("is_sign")
      let is_pre = this.getFormOption("is_pre")
      // 是否启用电子合同节点
      let isEcontract = this.getFormOption("is_econtract")
      let mall_supplier = this.getFormOption("signcode")
      let econtractcode = this.getFormOption("econtractcode")
      // 营地类型切换方便timeContainer
      let campsiteType = this.getFormOption("campsite_type")
      // 营期
      let timeContainer = this.getcolumnsOption("timeContainer")
      // 报名截止时间
      let deadlinetime = this.getcolumnsOption("deadlinetime")
      // 期次名称
      let phaseName = this.getcolumnsOption("phase_name")
      // 地址
      let position = this.getcolumnsOption("position")
      //let mall_supplier = this.getFormOption("mall_supplier")
      this.editFormFields.is_pre = 0
      // 报名单切换事件
      smsId.onChange = (val, option) => {
        if (val !== 1) {
          console.log(val)
          mall_supplier.hidden = true
        } else {
          mall_supplier.hidden = false
        }

      }
      // 电子合同切换事件
      isEcontract.onChange = (val, option) => {
        this.editFormFields.econtractcode =''
        if (val !== 1) {
          econtractcode.hidden = true;
          econtractcode.required = false;
        } else {
          econtractcode.hidden = false;
          econtractcode.required = true;
        }
      }
      let is_open = this.getFormOption('is_open')
      let distributorNews_id = this.getFormOption('distributorNews_id')
      let pram = { "page": 1, "rows": 30, "sort": "createDate", "order": "desc", "wheres": "[{\"name\":\"distributorType\",\"value\":\"2004\",\"displayType\":\"select\"}]" }
      this.http.post("/api/DistributorNews/getPageData", pram).then(res => {
        distributorNews_id.data = res.rows.map(i => {
          return {
            key: i.id,
            value: i.distributorName
          }
        })
      })
      is_open.onChange = (v) => {
        if (v == 1) {
          distributorNews_id.hidden = false
        } else {
          distributorNews_id.hidden = true
        }
      }
      // 营地类型切换事件
      campsiteType.onChange = (val, option) => {
        // 冬令营
        if (val == 1) {
          timeContainer.title = "营期"
          phaseName.title = "期次名称"
          deadlinetime.hidden = false;
        } else if (val == 2) {   //房车
          timeContainer.title = "营位"
          phaseName.title = "名称"

          deadlinetime.hidden = true;
        } else if (val == 3) {    //营位置出租
          phaseName.title = "名称"
          timeContainer.title = "营位"
          deadlinetime.hidden = true;
        }

      }
      //操作从表
      this.detail.columns.push({
        title: "操作", width: 100, align: "center", render: (h, { row, column, index }) => {
          return h(
            "div",
            { style: {} },
            [h(
              "Button",
              {
                style: { "margin-right": "10px" },
                on: {
                  click: (e) => {
                    console.log("详情", row);
                    this.$refs.gridBody.open(row, index)
                  }
                }
              },
              "添加行程"
            ),
            h(
              "Button",
              {
                style: { "margin-right": "10px" },
                on: {
                  click: (e) => {
                    this.$refs.gridBody.open1(row, index)
                  }
                }
              },
              "设置"
            ),


            ],
          )
        }
      })
      let list = []
      let pa = {
        order: "desc",
        page: 1,
        rows: 1000,
        sort: "id",
        wheres: "[]"
      }
      this.http.post('/api/CampsiteLabel/getPageData', pa).then(res => {
        list = res.rows
      })

      function upLabelId (params) {
        let l = params.split(',')
        let ar = []
        l.forEach(k => {
          list.forEach(i => {
            if (i.id == k) {
              ar.push(i.name)
            }
          })
        })
        return ar.join('、')
      }
      this.columns.forEach(i => {
        if (i.field == 'labelId') {
          i.render = (h, { row, column, index }) => {
            return h(
              "div", {},
              row.labelId != null ? upLabelId(row.labelId) : ''
            )
          }
        }
      })
      this.columns.push({
        title: '操作',
        fixed: 'right',
        width: 80,
        render: (h, { row, column, index }) => {
          return h(
            "div", { style: {} }, [
            h(
              "a", {
              props: {},
              style: {},
              on: {
                click: (e) => {
                  this.$refs.gridBody.CampsiteLabel(row)
                }
              }
            }, "打标签"),
            // h(
            //   "a", {
            //   props: {},
            //   style: {},
            //   on: {
            //     click: (e) => {
            //       this.$refs.gridBody.model = true
            //       this.$refs.gridBody.GetCampsiteRoomInfo(row)
            //     }
            //   }
            // },
            // row.campsite_type == 2 ? "查看营房" : ''
            // ),
          ])
        }
      })

    },
    modelOpenAfter (row) {
      let smsId = this.getFormOption("is_sign")
      let mall_supplier = this.getFormOption("signcode")
      let econtractcode = this.getFormOption("econtractcode")
      //let mall_supplier = this.getFormOption("mall_supplier")
      let distributorNews_id = this.getFormOption('distributorNews_id')
      if (row == undefined) {
        mall_supplier.hidden = false
        econtractcode.hidden = false
      } else {
        if (row.is_sign == 0) {
          mall_supplier.hidden = true
        } else if (row.is_sign == 1) {
          mall_supplier.hidden = false
        }
        if(row.is_econtract != 1){
          econtractcode.hidden = true
          econtractcode.required = false
        }else{
          econtractcode.hidden = false
          econtractcode.required = true
        }
      }
      if (this.currentAction == "Add") {
        this.editFormFields.is_pre = 0
        this.editFormFields.position = ''
        this.editFormFields.startAddress = ''
        this.editFormFields.endAddress = ''
        this.editFormFields.is_open = 0
      } else {
        if (row.is_open == 1) {
          distributorNews_id.hidden = false
        } else {
          distributorNews_id.hidden = true
        }
      }
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      //console.log(row,column,event);
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    addBefore (formData) { //新建保存前formData为对象，包括明细表
      return this.veriFromTable(formData)
    },
    updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.veriFromTable(formData)
    },
    // 保存前编辑
    veriFromTable (formData) {
      // 将从表中的日期切分为开始日期 和结束日期
      console.log(formData, "formData")
      try {
        formData.detailData.forEach(item => {
          console.log(item.timeContainer, item.timeContainer.length)

          //item.timeContainer = []
          if (item.timeContainer.length > 0) {
            item.begin_time = item.timeContainer[0]
            item.end_time = item.timeContainer[1]
          } else {
            item.begin_time = ""
            item.end_time = ""
          }

        })
    } catch (e) {
      this.$error("营期为必填项");
      return false
   }


      return true
    },

    searchDetailAfter (data) {//查询从表后param查询参数,result回返查询的结果
      console.log("从表数据", data);
      // 将从表中的开始日期 和 结束日期合并为起止日期
      data.forEach(item => {
        item.timeContainer = []
        item.timeContainer.push(item.begin_time)
        item.timeContainer.push(item.end_time)
      })
      return true;
    },
  }
};
export default extension;
